package com.fangyi.yunmusic.interceptor;

import com.fangyi.yunmusic.utils.JsonResult;
import com.fangyi.yunmusic.utils.JsonUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

public class BaseInterceptor implements HandlerInterceptor{
    protected void returnErrorResponse(HttpServletResponse response, JsonResult result) throws IOException {
        OutputStream out = null;
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/json");
            out = response.getOutputStream();
            out.write(JsonUtils.objectToJson(result).getBytes("utf-8"));
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
